<?php
require_once 'Kanedo/Config/Ini.php';
class Kanedo_Translate 
{
	/**
	 * Uebersetzungs art, ob ni oder DB
	 *
	 * @var string
	 */
	protected $_mode = "ini";
	/**
	 * Pfad zur Uebersetzungsdatei
	 *
	 * @var string
	 */
	protected $_configIni;
	/**
	 * Instanz der Kanedo_Db
	 *
	 * @var Kanedo_Db
	 */
	protected $_configDb;
	/**
	 * Kanedo_Language
	 *
	 * @var Kanedo_Language
	 */
	protected $_lang = null;
	 
	public function __construct($mode, Kanedo_Language $lang)
	{
		switch($mode)
		{
			case 'ini':
				$this->mode = 'ini';
				break;
			case 'db':
				$this->mode = 'db';
				break;
			default:
				throw new Kanedo_Language_Exception('Unknown translation Mode');
				break;
				
		}
		
		$this->_lang = $lang;

	}
	
	public function configure($config)
	{
		if($this->_mode == 'ini')
		{
			if(file_exists($config))
			{
				$this->_configIni = $config;
			}else {
				throw new Kanedo_Language_Exception("Can't find the translation file!");
			}	
		}elseif($this->_mode == 'db')
		{
			if(!$config instanceof Kanedo_Db) {
				throw new Kanedo_Language_Exception("For Databaseconnection you need a instance of Kanedo_Db");
			}
			$this->_configDb = $config;
		}
		
	}
	
	public function _($string)
	{
		if($this->_mode == 'ini')
		{
			return $this->translateIni($string);
		}
	}
	
	protected function translateIni($string)
	{
		$ini = new Kanedo_Config_Ini($this->_configIni);
		$lang = $this->_lang->getCurrentLanguage();
		$translations = $ini->getSection($lang);
		if (key_exists($string, $translations))
		{
			return $translations[$string];		
		}else {
			throw new Kanedo_Language_Exception('Unknown Translationcode!('.$string.")");
		}
	}
}
?>